package org.yxj.lib_storage;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;

import java.io.File;

/**
 * 存储管理类
 *
 * @author on 2023/10/30
 */
public final class StorageFileManager {


    private Context context;

    /**
     * app 存储根目录文件夹名字
     */
    private String ROOT_FILE_NAME;
    private SDCardUtil sdCardUtil;

    /**
     * 保存图片
     */
    private SavePictureUtil savePictureUtil;

    /**
     * 保存视频到相册
     */
    private SaveVideoUtil saveVideoUtil;


    private static StorageFileManager manager;


    private StorageFileManager() {

    }


    public static StorageFileManager newInstance() {
        if (manager == null) {
            synchronized (StorageFileManager.class) {
                if (manager == null) {
                    manager = new StorageFileManager();
                }
            }
        }
        return manager;
    }


    public StorageFileManager isDebug(boolean b) {
        LogUtils.newInstance().init(b);
        return StorageFileManager.this;
    }


    /**
     * 初始化
     *
     * @param context
     */
    public void init(Context context) {
        this.context = context;
        if (isCheckRootFileName()) {
            throw new NullPointerException("文件的根目录名字不能为空，请通过setRootFileName()设置根目录名字，不可以为空");
        }
        sdCardUtil = new SDCardUtil(context, ROOT_FILE_NAME);
        savePictureUtil = new SavePictureUtil(context);
        saveVideoUtil = new SaveVideoUtil(context);
    }


    /**
     * 检测ROOT_FILE_NAME是否未设置
     */
    private boolean isCheckRootFileName() {
        return TextUtils.isEmpty(ROOT_FILE_NAME);
    }


    /**
     * 设置目录文件夹名字
     *
     * @param ROOT_FILE_NAME
     * @return
     */
    public StorageFileManager setRootFileName(String ROOT_FILE_NAME) {
        this.ROOT_FILE_NAME = ROOT_FILE_NAME;
        return StorageFileManager.this;
    }


    /**
     * 获取根目录路径
     *
     * @return
     */
    public String rootPath() {
        if (sdCardUtil == null) {
            throw new NullPointerException("sdCardUtil is null");
        }
        return sdCardUtil.getRootPath();
    }


    public String getImgPath() {
        if (sdCardUtil == null) {
            throw new NullPointerException("sdCardUtil is null");
        }
        return sdCardUtil.getImgPath();
    }

    /**
     * 获取根目录路径
     *
     * @return
     */
    public String rootPathName() {
        if (sdCardUtil == null) {
            throw new NullPointerException("sdCardUtil is null");
        }
        return sdCardUtil.getROOT_FILE_NAME();
    }


    /**
     * 保存图片到相册
     *
     * @param bitmap
     * @param callBack
     */
    public void saveBitmapToPhotoAlbum(Bitmap bitmap, CallBack<SaveImageBean> callBack) {
        if (savePictureUtil == null) {
            throw new NullPointerException("savePictureUtil is null");
        }
        savePictureUtil.androidSaveBitmap(bitmap, callBack);
    }


    /**
     * 保存视频到相册
     *
     * @param file
     */
    public void saveVideoToPhotoAlbum(File file) {
        if (saveVideoUtil == null) {
            throw new NullPointerException("saveVideoUtil is null");
        }
        saveVideoUtil.androidSaveVideo(file);
    }


    /**
     * uri 转成 File
     *
     * @param context
     * @param uri
     * @param callBack
     */
    public void uriToFile(Context context, Uri uri, CallBack<File> callBack) {
        FileUtils.saveUriToFile(context, uri, sdCardUtil.getImgPath(), callBack);
    }



    /**
     * @return
     */
    public String getRoot(){
        if (saveVideoUtil == null) {
            throw new NullPointerException("saveVideoUtil is null");
        }
        return sdCardUtil.getRoot();
    }

}
